package cn.edu.nju.setting;

import java.util.List;

import cn.edu.nju.R;

import android.app.Activity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.RelativeLayout;
import android.widget.TextView;

public class ModifiableTagAdapter extends BaseAdapter{
	private Activity context;
	private LayoutInflater inflater;
	private List<String> tags;
	
	public ModifiableTagAdapter(Activity activity, List<String> tags){
		this.context = activity;
		this.tags = tags;
		this.inflater = context.getLayoutInflater();
	}
	
	public int getCount() {
		return tags.size();
	}

	public Object getItem(int position) {
		return tags.get(position);
	}

	public long getItemId(int position) {
		return position;
	}

	public View getView(int position, View convertView, ViewGroup parent) {
		RelativeLayout tagLayout;
		if(convertView == null){
			tagLayout = (RelativeLayout)inflater.inflate(R.layout.modify_tag, null); 
		}
		else{
			tagLayout = (RelativeLayout)convertView;
		}
		
		TextView tagText = (TextView)tagLayout.findViewById(R.id.modify_tag_edit);
		tagText.setText(tags.get(position));
		
		Button deleteTagButton = (Button)tagLayout.findViewById(R.id.modify_tag_delete);
		deleteTagButton.setTag(R.id.VIEW_POSITION_TAG, position);
		deleteTagButton.setOnClickListener(new OnClickListener() {			
			public void onClick(View v) {
				int position = (Integer)v.getTag(R.id.VIEW_POSITION_TAG);
				tags.remove(position);
				notifyDataSetChanged();
			}
		});
		
		return tagLayout;
	}
}
